<template>
  <div>
    <!-- 搜索 -->
    <!-- 车型、工时单价关系维护 -->
    <TopFilter :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchapi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
    >
    <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" icon="el-icon-check" :click="qiyong.bind()" :disabled="disableds">启用</multiuse-button>
          <multiuse-button size="small" type="primary" icon="el-icon-close" :click="tingyong.bind()" :disabled="disableds">禁用</multiuse-button>
          <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
          <multiuse-button type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
          <multiuse-button size="small" type="primary" :click="downloadTemplate.bind()">下载模板</multiuse-button>
          <multiuse-button size="small" icon="el-icon-upload" @click="showImportFun()">导入</multiuse-button>
    </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="'车型新增'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="close" @save="save"></vehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="emitVisible" title="车型、工时单价关系" width="920" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <editForm @close="emitCloseHandle" @save="emitChange" :rowData="rowData"/>
    </BaseDialog>  
    <BaseDialog :visible.sync="visibleAdd" :title="'车型、工时单价关系新增'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </BaseDialog>
    <BaseDialog :visible.sync="showImport" :closable="false" icon="el-icon-upload" title="导入" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">
      <importExcel @close="showImport = false" @importclose="editsave()" url="/api/cms_claimfac/rPModelLabour/importExcel"/>
    </BaseDialog>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
    </div>

  </div>
</template>
<script>
import vehicle  from '@/pages/claimDemage/tools/ModelVehicle.vue';
import AddForm from './AddForm.vue';
import editForm from './editForm.vue';
import { getListByPage,deleteOne, piliangqiyong, piliangtingyong,getListForExport,downloadModel,exporturl} from '@/api/claim/factory/ModelAndLabour';
import { exportExcelFile } from '@/utils/index';
import importExcel from '@/pages/claimDemage/tools/importExcel.vue';
export default {
  // mixins: [dictionary],
    components: {
        vehicle,
        AddForm,
        editForm,
        importExcel
  },
  data() {
    return {
        visible: false,
        dataList: [],
        topFilterInit: this.topFilterInitFun(),
        columns: this.creatHeader(),
        collapse: false,
        rowData: {},
        disableds: true,
        fetchParams: {
          xhrAbort: true
        }, //ipa 接口
        fetchapi:getListByPage,
        emitVisible: false,
        visibleAdd: false,
        showImport: false,
    };
  },
  created() {
  },
  methods: {
     editsave() {
      this.showImport = false;
      this.fetchParams = { ...this.fetchParams };
    },
    // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    //下载模板
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        downloadModel().then((res) => {
          console.log(res);
          exportExcelFile(res.data, '车型、工时单价关系维护导入模板');
        });
      });
    },
      //导出
     async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000130' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    //批量启用
    async qiyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map((x) => {
          str += x.mlId + ',';
        });
        const res = await piliangqiyong({ ids: str, status: '1' });
        this.fetchParams = { ...this.fetchParams };
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请勾选后再进行启用！' });
      }
    },
    //批量停用
    async tingyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map((x) => {
          str += x.mlId + ',';
        });
        const res = await piliangtingyong({ ids: str, status: '0' });
        this.fetchParams = { ...this.fetchParams };
        this.loading = false;
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请勾选后再进行禁用！' });
      }
    },
    // 删除
    async delFun(data) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(data.mlId)
          this.res = await deleteOne({mlId: data.mlId});
          if (this.res.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '删除成功!' });
            // 重新加载
            this.fetchParams = { ...this.fetchParams };
          }
        })
    },
    //新增
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    chooseOrderNo() {
      this.visible = true;
    },
    close() {
      this.visible = false;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.visible = false;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      this.disableds = this.selectedRows.length <= 0;
      console.log(this.selectedRows);
    },
    emitFun(data) {
      this.rowData = data;
      this.emitVisible = true;
    },
    emitCloseHandle() {
      this.emitVisible = false;
    },
    emitChange(val) {
      this.emitVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
            type: 'INPUT',
            label: '车型',
            fieldName: 'modelCode',
            placeholder: '请输入车型...',
            initialValue: '',
            unitRender: p => {
              return (
                <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
            }
        },
        {
          type: 'INPUT',
          label: '工时单价',
          fieldName: 'cmsClaimPrice',
          placeholder: '请输入工时单价',
          // disabled: true,
          unitRender: (p) => {
            return <div>元</div>;
          }
        }

      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams,{xhrAbort: false},val);
    },
    // //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.emitFun(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '状态', dataIndex: 'statusStr', sorter: true },
        { title: '车型代码', dataIndex: 'modelCode', sorter: true },
        { title: '车型名称', dataIndex: 'modelName', sorter: true },
        { title: '工时单价', dataIndex: 'cmsClaimPrice', sorter: true },
        ];
    }
  }

};
</script>

<style lang="less" scoped></style>
